package com.yiren.backstage.db.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;

import com.yiren.backstage.currency.entity.JudgeDetailsEntity;
import com.yiren.backstage.currency.entity.JudgeEntity;
import com.yiren.backstage.currency.entity.StateMonitorEntity;

import com.yiren.backstage.currency.entity.JudgeDetailsEntity;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "judge_details".
*/
public class JudgeDetailsEntityDao extends AbstractDao<JudgeDetailsEntity, String> {

    public static final String TABLENAME = "judge_details";

    /**
     * Properties of entity JudgeDetailsEntity.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, String.class, "id", true, "ID");
        public final static Property ParentId = new Property(1, String.class, "parentId", false, "PARENT_ID");
        public final static Property JudgeId = new Property(2, String.class, "judgeId", false, "JUDGE_ID");
        public final static Property StartVoiceName = new Property(3, String.class, "startVoiceName", false, "START_VOICE_NAME");
        public final static Property StartContent = new Property(4, String.class, "startContent", false, "START_CONTENT");
        public final static Property ConditionValue = new Property(5, float.class, "conditionValue", false, "CONDITION_VALUE");
        public final static Property ConditionElement = new Property(6, String.class, "conditionElement", false, "CONDITION_ELEMENT");
        public final static Property ConditionType = new Property(7, String.class, "conditionType", false, "CONDITION_TYPE");
        public final static Property StateMonitorId = new Property(8, String.class, "stateMonitorId", false, "STATE_MONITOR_ID");
        public final static Property ExecuteValue = new Property(9, float.class, "executeValue", false, "EXECUTE_VALUE");
        public final static Property ExecuteElement = new Property(10, String.class, "executeElement", false, "EXECUTE_ELEMENT");
    }

    private DaoSession daoSession;


    public JudgeDetailsEntityDao(DaoConfig config) {
        super(config);
    }
    
    public JudgeDetailsEntityDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, JudgeDetailsEntity entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String parentId = entity.getParentId();
        if (parentId != null) {
            stmt.bindString(2, parentId);
        }
 
        String judgeId = entity.getJudgeId();
        if (judgeId != null) {
            stmt.bindString(3, judgeId);
        }
 
        String startVoiceName = entity.getStartVoiceName();
        if (startVoiceName != null) {
            stmt.bindString(4, startVoiceName);
        }
 
        String startContent = entity.getStartContent();
        if (startContent != null) {
            stmt.bindString(5, startContent);
        }
        stmt.bindDouble(6, entity.getConditionValue());
 
        String conditionElement = entity.getConditionElement();
        if (conditionElement != null) {
            stmt.bindString(7, conditionElement);
        }
 
        String conditionType = entity.getConditionType();
        if (conditionType != null) {
            stmt.bindString(8, conditionType);
        }
 
        String stateMonitorId = entity.getStateMonitorId();
        if (stateMonitorId != null) {
            stmt.bindString(9, stateMonitorId);
        }
        stmt.bindDouble(10, entity.getExecuteValue());
 
        String executeElement = entity.getExecuteElement();
        if (executeElement != null) {
            stmt.bindString(11, executeElement);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, JudgeDetailsEntity entity) {
        stmt.clearBindings();
 
        String id = entity.getId();
        if (id != null) {
            stmt.bindString(1, id);
        }
 
        String parentId = entity.getParentId();
        if (parentId != null) {
            stmt.bindString(2, parentId);
        }
 
        String judgeId = entity.getJudgeId();
        if (judgeId != null) {
            stmt.bindString(3, judgeId);
        }
 
        String startVoiceName = entity.getStartVoiceName();
        if (startVoiceName != null) {
            stmt.bindString(4, startVoiceName);
        }
 
        String startContent = entity.getStartContent();
        if (startContent != null) {
            stmt.bindString(5, startContent);
        }
        stmt.bindDouble(6, entity.getConditionValue());
 
        String conditionElement = entity.getConditionElement();
        if (conditionElement != null) {
            stmt.bindString(7, conditionElement);
        }
 
        String conditionType = entity.getConditionType();
        if (conditionType != null) {
            stmt.bindString(8, conditionType);
        }
 
        String stateMonitorId = entity.getStateMonitorId();
        if (stateMonitorId != null) {
            stmt.bindString(9, stateMonitorId);
        }
        stmt.bindDouble(10, entity.getExecuteValue());
 
        String executeElement = entity.getExecuteElement();
        if (executeElement != null) {
            stmt.bindString(11, executeElement);
        }
    }

    @Override
    protected final void attachEntity(JudgeDetailsEntity entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public String readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
    }    

    @Override
    public JudgeDetailsEntity readEntity(Cursor cursor, int offset) {
        JudgeDetailsEntity entity = new JudgeDetailsEntity( //
            cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // parentId
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // judgeId
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // startVoiceName
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // startContent
            cursor.getFloat(offset + 5), // conditionValue
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // conditionElement
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // conditionType
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // stateMonitorId
            cursor.getFloat(offset + 9), // executeValue
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10) // executeElement
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, JudgeDetailsEntity entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0));
        entity.setParentId(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setJudgeId(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setStartVoiceName(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setStartContent(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setConditionValue(cursor.getFloat(offset + 5));
        entity.setConditionElement(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setConditionType(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setStateMonitorId(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setExecuteValue(cursor.getFloat(offset + 9));
        entity.setExecuteElement(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
     }
    
    @Override
    protected final String updateKeyAfterInsert(JudgeDetailsEntity entity, long rowId) {
        return entity.getId();
    }
    
    @Override
    public String getKey(JudgeDetailsEntity entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(JudgeDetailsEntity entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getJudgeDetailsEntityDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getJudgeEntityDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T2", daoSession.getStateMonitorEntityDao().getAllColumns());
            builder.append(" FROM judge_details T");
            builder.append(" LEFT JOIN judge_details T0 ON T.\"PARENT_ID\"=T0.\"ID\"");
            builder.append(" LEFT JOIN judge T1 ON T.\"JUDGE_ID\"=T1.\"ID\"");
            builder.append(" LEFT JOIN state_monitor T2 ON T.\"STATE_MONITOR_ID\"=T2.\"ID\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected JudgeDetailsEntity loadCurrentDeep(Cursor cursor, boolean lock) {
        JudgeDetailsEntity entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        JudgeDetailsEntity parentJudgeDetails = loadCurrentOther(daoSession.getJudgeDetailsEntityDao(), cursor, offset);
        entity.setParentJudgeDetails(parentJudgeDetails);
        offset += daoSession.getJudgeDetailsEntityDao().getAllColumns().length;

        JudgeEntity judgeEntity = loadCurrentOther(daoSession.getJudgeEntityDao(), cursor, offset);
        entity.setJudgeEntity(judgeEntity);
        offset += daoSession.getJudgeEntityDao().getAllColumns().length;

        StateMonitorEntity stateMonitorEntity = loadCurrentOther(daoSession.getStateMonitorEntityDao(), cursor, offset);
        entity.setStateMonitorEntity(stateMonitorEntity);

        return entity;    
    }

    public JudgeDetailsEntity loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<JudgeDetailsEntity> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<JudgeDetailsEntity> list = new ArrayList<JudgeDetailsEntity>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<JudgeDetailsEntity> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<JudgeDetailsEntity> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
